var quantity = '';

$(() => {
	loadCartList();
	initCartListener();
})

initCartListener = () => {
	$(".settlement").on('click', (e) => {
		onPrevPay()
	});
	$("#cart_edit").on('click', (e) => {
		onCartEdit()
	});
	$('.check_all').change(function(e) {
		if (this.checked) {
			$.each($('.cart_check_list'), function(e) {
				this.checked = true
			});
			$.each($('.check_child_input'), function(e) {
				this.checked = true
			});

		} else {
			$.each($('.cart_check_list'), function(e) {
				this.checked = false
			});
			$.each($('.check_child_input'), function(e) {
				this.checked = false
			});
		}
		onCount();
	})
}

onCartEdit = () => {

}

onPrevPay = () => {
	let total = $('#total').html();
	if (Number(total) > 0) {
		showActionDialog('支付信息', `总共需要支付:${total}元!`, res => {
			let subLoading = layer.open({
				type: 2,
				shadeClose: false,
				content: '正在支付...'
			});

			setTimeout(() => {
				layer.close(subLoading);
				showToast('支付成功！');
				setDefaultState();
			}, 500)
		})
	}
}

setDefaultState = () => {
	$('#cart_list').html('');
	$('#total').html('0');
	$('#quilty').html('0');
	$(".settlement").html(`去结算`);
	$('.check_all').prop('checked', false);
	$(".cart_footer").hide();
	$(".cart_header").hide();
	$("#load_layout_cart").show();
}

loadCartList = () => {
	queryCartList(res => {
		setCartList(res);
	});
}

setCartList = (mlist) => {
	$("#cart_list").html('');
	let itemHtml = ``;
	for (var i = 0; i < mlist.length; i++) {
		let obj = mlist[i];
		let promotionGoodsList = obj.promotionGoodsList;

		itemHtml += `<div class="cart_list">
						<label class="custom-checkbox ">
							<input type="checkbox" class="cart_check_list" onchange="onListCheckboxChange(this)"/>
							<span class="checkmark"></span>
							<span class="checkbox_text">${obj.shopName}</span>
						</label>`;

		for (var j = 0; j < promotionGoodsList.length; j++) {
			let childObj = promotionGoodsList[j];
			itemHtml += `<label class="custom-checkbox top_10 cart_check_child" >
							<input type="checkbox" data-price="${childObj.price}" class="check_child_input" onchange="onListChildCheckboxChange(this)"/>
							<span class="checkmark"></span>
							<img src="${childObj.thumb}" class="cart_list_img" />
							<div class="cart_list_content">
								<span class="title">${childObj.title}</span>
								<div class="top">
									<span class="tag">${childObj.specInfo[0].specValue},${childObj.specInfo[1].specValue}</span>
								</div>
								<div class="footer_box">
									<div class="goods_price">
										<span class="unit">¥</span>
										<span class="price">${childObj.price}</span>
									</div>
									<span class="flex_1"></span>
									<input type="button" value="-" class="btn_sub" onclick="onCartSub(this)"/>
									<span class="number" onclick="onInputQuantity(this)">${childObj.quantity}</span>
									<input type="button" value="+" class="btn_sub" onclick="onCartAdd(this)"/>
								</div>
							</div>
						</label>`
		}
		itemHtml += `</div>`;
	}
	$("#cart_list").html(itemHtml);
}

inputQuantity = (e) => {
	quantity = e.value;
}

onInputQuantity = (e) => {
	event.stopImmediatePropagation(); //阻止事件分发
	let inputHtml = `<input placeholder="请输入" class="form_input" type="number" oninput="inputQuantity(this)"/>`;
	layer.open({
		title: [
			'输入数量',
			'background-color:white; color:#FF34B3;'
		],
		anim: 'up',
		shadeClose: false,
		content: inputHtml,
		btn: ['确定', '取消'],

		yes: function(index, layero) {
			if (isEmpty(quantity)) {
				showToast('请输入')
			} else {
				layer.close(index);
				$(e).html(quantity);
				onCount();
			}
			return false;
		}
	});
}

onCartSub = (e) => {
	let numberEle = $(e).next('.number');
	let num = Number(numberEle.html());
	if (num > 0) {
		numberEle.html(num - 1);
		onCount();
	}
}

onCartAdd = (e) => {
	let numberEle = $(e).prev('.number');
	let num = Number(numberEle.html());
	numberEle.html(num + 1);
	onCount();
}

onListCheckboxChange = (e) => {
	event.stopImmediatePropagation(); //阻止事件分发
	let parents = $(e).parent();
	let siblings = $(parents).siblings();
	$(siblings).find('.check_child_input').each(function(eve) {
		if (e.checked) {
			this.checked = true;
		} else {
			this.checked = false;
			$('.check_all').prop('checked', false);
		}
	});
	onCount();
}

onListChildCheckboxChange = (e) => {
	event.stopImmediatePropagation(); //阻止事件分发
	let _this = $(e);
	// if ($(e).attr("checked") == undefined) {
	// 	$(e).attr("checked", "checked");
	// } else {
	// 	$(e).removeAttr("checked");
	// }
	if (_this.checked) {
		_this.checked = false;
	} else {
		_this.checked = true;
		$('.check_all').prop('checked', false);
	}
	onCount();
}

onCount = () => {
	setTimeout(function() {

		var total = 0,
			quantity = 0;
		$.each($('.check_child_input'), function(e) {
			console.log(this.checked)
			let price = this.dataset.price;
			let _this = $(this);
			let currentParent = _this.parent();
			let num = $(currentParent).find('.number').html();
			if (this.checked) { //只有选中的才计算
				total += Number(price) * Number(num);
				quantity += Number(num);
			}
		});
		$("#total").html(total.toFixed(1));
		$('#quilty').html(quantity);
		if (quantity > 0) {
			$(".settlement").html(`去结算(${quantity})`);
		} else {
			$(".settlement").html(`去结算`);
		}

	}, 100)
}
